package com.googlecode.jlsf;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

/**
 * jlsf descriptor
 * @author Alexander Chaychuk
 */
public class Config extends Properties {

	private static final long serialVersionUID = -5041972010507782984L;

	// Default properties
	//
	private int pingTries = 2;

	private int pingTimeout = 300;

	private int pingThreadsAmount = 5;

	private int[] shoutcastPorts = { 8000 };

	private int shoutcastTimeout = 1500;

	private static Config info = null;

	// Singelton. 
	//
	public static synchronized Config getInstance() 
	{
		if (info == null)
			return new Config();
		else 
			return info;
	}
	
	private Config() {
		
		super();
		info = this;
		
		if(LoadFile())
			{
				pingTries = Integer.parseInt(this.getProperty("ping_tries"));
				pingTimeout = Integer.parseInt(this.getProperty("ping_timeout"));
				pingThreadsAmount = Integer.parseInt(this.getProperty("ping_threads_amount"));
				shoutcastTimeout = Integer.parseInt(this.getProperty("shoutcast_timeout"));
				shoutcastPorts = getShoutcastPorts(this.getProperty("shoutcast_ports"));
			}
	}

	// Load properties file
	//
	private boolean LoadFile() {
		try {
			// HACK: 
			this.load(new FileInputStream("jlsf.properties"));
			
			return true;

		} catch (FileNotFoundException e) {

			System.err.println("File not found. Used default properties.");

		} catch (IOException e) {

			System.err.println("File not found. Used default properties.");
		}
		
		return false;
	}

	
	// Parse string and returned array of ports
	//
	private int[] getShoutcastPorts(String propertyItem) {

		String tokens[] = propertyItem.split(",");

		int portList[] = new int[tokens.length];

		for (int i = 0; i < tokens.length; i++) {
			portList[i] = Integer.parseInt(tokens[i].trim());
		}

		return portList;
	}
	
	public int getPingTries()
	{
		return pingTries;
	}
	
	public int getPingTimeout()
	{
		return pingTimeout;
	}
	
	public int getPingThreadsAmount()
	{
		return pingThreadsAmount;
	}
	
	public int[] getShoutcastPorts()
	{
		return shoutcastPorts;
	}
	
	public int getShoutcastTimeout()
	{
		return shoutcastTimeout;
	}

}
